package com.liqi.display.service.impl;

import com.liqi.display.entity.TblTakeNumber;
import com.liqi.display.entity.returnType.TotalTakeType;
import com.liqi.display.mapper.TblTakeNumberMapper;
import com.liqi.display.service.ITblTakeNumberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liqi.display.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liqi
 * @since 2021-04-13
 */
@Service
public class TblTakeNumberServiceImpl extends ServiceImpl<TblTakeNumberMapper, TblTakeNumber> implements ITblTakeNumberService {

    private Logger logger = LoggerFactory.getLogger(TblTakeNumberServiceImpl.class);
    @Autowired
    TblTakeNumberMapper takeNumberMapper;

    @Override
    public int total(Date date) {
        return takeNumberMapper.total(date);
    }

    /**
     * 当天与昨天的取票数比较
     */
    @Override
    public Map<String,String> compareDay() {
        Date endCurrentDay = DateUtil.getCurrentDay();//当前具体时间
        Date startday = DateUtil.getDay();//当天的零时零分零秒
        int currentTotal = takeNumberMapper.compare(startday, endCurrentDay);

        Date endYesterdayCurrent = DateUtil.getYesterdayCurrent();//昨天的当前时间
        Date startYesterday = DateUtil.getYesterday();//昨天的零时零分零秒
        int yesterdayTotal = takeNumberMapper.compare(startYesterday, endYesterdayCurrent);

        Map<String,String> map =new ConcurrentHashMap<>();
        if (currentTotal-yesterdayTotal>=0){
            map.put("compareDay","up");
        }else {
            map.put("compareDay", "down");
        }
        return map;
    }

    /**
     * 当月与上个月的取票数比较
     */
    public  Map<String,String> compareMonth(){
        Date endBeforeMonth = DateUtil.getBeforeMonthCurrent();//获取上个月的当前时间
        Date startBeforeMonth = DateUtil.getBeforeMonthFirst();//获取上个月的一号零时零分零秒
        int beforeMonthTotal = takeNumberMapper.compare(startBeforeMonth, endBeforeMonth);

        Date startMonth = DateUtil.getMonth();//获取本月一月一号零时零分零秒
        Date currentDay = DateUtil.getCurrentDay();//当前具体时间
        int monthTotal = takeNumberMapper.compare(startMonth, currentDay);
        Map<String,String> map =new ConcurrentHashMap<>();
        if (monthTotal-beforeMonthTotal>=0){
            map.put("compareMonth","up");
        }else {
            map.put("compareMonth", "down");
        }
        return map;
    }

    /**
     * 今年与上一年的取票数比较
     * @return
     */
    public  Map<String,String> compareYear(){
        Date endBeforeYear = DateUtil.getBeforeYear();//获取上年的当前时间
        Date startBeforeYear = DateUtil.getBeforeYearFirst();//获取上年的一月一号零时零分零秒
        int beforeYearTotal = takeNumberMapper.compare(startBeforeYear, endBeforeYear);

        Date startYear = DateUtil.getYear();//获取本年一月一号零时零分零秒
        Date currentDay = DateUtil.getCurrentDay();//当前具体时间
        int yearTotal = takeNumberMapper.compare(startYear, currentDay);
        Map<String,String> map =new ConcurrentHashMap<>();
        if (yearTotal-beforeYearTotal>=0){
            map.put("compareYear","up");
        }else {
            map.put("compareYear", "down");
        }
        return map;
    }

    @Override
    public int compareEveryDay(Date start, Date end) {
        return takeNumberMapper.compare(start,end);
    }

    /**
     *
     * @param date 最近的日期，如：最近7天的开始时间、最近30天的开始时间
     * @param num 9：代表9点-10点，10：代表10点-11点,11代表 11点-12点，以此类推
     * @return
     */
    @Override
    public int lateComare(Date date,Integer num) {
        if (num == 9) {
            return takeNumberMapper.lateComare9(date);
        } else if (num == 10) {
            return takeNumberMapper.lateComare10(date);
        } else if (num == 11) {
            return takeNumberMapper.lateComare11(date);
        } else if (num == 14) {
            return takeNumberMapper.lateComare14(date);
        } else if (num == 15) {
            return takeNumberMapper.lateComare15(date);
        } else if (num == 16) {
            return takeNumberMapper.lateComare16(date);
        } else {
            return takeNumberMapper.lateComare8(date);
        }
    }

    /**
     * 统计当月的取票方式  (从当前月份的1号零时零分零秒开始）
     * @return
     * 0是取号机、1是预约、2微信、3呼叫转移、4绿色通道、5跨网点终端取号  6、特殊群体
     */

    @Override
    public  Map<String,Integer> totalTakeType() {
        Map<String,Integer> map = new ConcurrentHashMap<String,Integer>();
        try {
            List<TotalTakeType> totalTakeTypes = takeNumberMapper.totalTakeType(DateUtil.getMonth());
            if(totalTakeTypes.size() != 0){
                for (TotalTakeType t:totalTakeTypes){
                    map.put(t.getTakeType(),t.getNum());
                }
                if (map.get("0") == null){//取号机
                    map.put("0",0);
                }
                if (map.get("1") == null){//预约
                    map.put("1",0);
                }
                if (map.get("2") == null){//微信
                    map.put("2",0);
                }
                if (map.get("3")==null){//呼叫转移
                    map.put("3",0);
                }
                if (map.get("4")==null){//绿色通道
                    map.put("4",0);
                }
                if (map.get("5")==null){//跨网点终端取号
                    map.put("5",0);
                }
                return map;
            }else {
                map.put("0",2);
                map.put("1",4);
                map.put("2",6);
                map.put("3",1);
                map.put("4",2);
                map.put("5",3);
                map.put("6",4);
            }
            return map;
        }catch (Exception e){
            map.put("0",2);
            map.put("1",4);
            map.put("2",6);
            map.put("3",1);
            map.put("4",2);
            map.put("5",3);
            map.put("6",4);
            return map;
        }

    }

    //统计预约取号数和现场取号数
    @Override
    public Map<String, Integer> totalOrAndCur() {
        Map<String,Integer> map = new ConcurrentHashMap<>();
        try {
            List<TotalTakeType> totalTakeTypes = takeNumberMapper.totalTakeType(DateUtil.getDay());//当天
            int currentDayTotal = 0;
            if(totalTakeTypes.size() != 0){
                for (TotalTakeType t:totalTakeTypes){
                    if ("0".equals(t.getTakeType())){//取号机
                        currentDayTotal+=t.getNum();
                    }
                    if ("1".equals(t.getTakeType())){//预约
                        map.put("orderDayTotal",t.getNum());
                    }
                    if ("2".equals(t.getTakeType())){//微信
                        map.put("webchatDayTotal",t.getNum());
                    }
                    if ("3".equals(t.getTakeType())){//呼叫转移
                        currentDayTotal+=t.getNum();
                    }
                    if ("4".equals(t.getTakeType())){//绿色通道
                        currentDayTotal+=t.getNum();
                    }
                    if ("5".equals(t.getTakeType())){//跨点取号
                        currentDayTotal+=t.getNum();
                    }
                    if ("6".equals(t.getTakeType())){//特殊群体
                        currentDayTotal+=t.getNum();
                    }
                    map.put("currentDayTotal",currentDayTotal);
                }
            }
            //当月
            int notWorkDay = DateUtil.getNotWorkDay(30);
            List<TotalTakeType> totalTakeType = takeNumberMapper.totalTakeType(DateUtil.getLateDate("yyyy-MM-dd HH:mm:ss", 30+notWorkDay));
            int currentMonthTotal = 0;
            if(totalTakeType.size() != 0){
                for (TotalTakeType t:totalTakeType){
                    if ("0".equals(t.getTakeType())){//取号机
                        currentMonthTotal+=t.getNum();
                    }
                    if ("1".equals(t.getTakeType())){//预约
                        map.put("orderMonthTotal",t.getNum());
                    }
                    if ("2".equals(t.getTakeType())){//微信
                        map.put("webchatMonthTotal",t.getNum());
                    }
                    if ("3".equals(t.getTakeType())){//呼叫转移
                        currentMonthTotal+=t.getNum();
                    }
                    if ("4".equals(t.getTakeType())){//绿色通道
                        currentMonthTotal+=t.getNum();
                    }
                    if ("5".equals(t.getTakeType())){//跨点取号
                        currentMonthTotal+=t.getNum();
                    }
                    if ("6".equals(t.getTakeType())){//特殊群体
                        currentMonthTotal+=t.getNum();
                    }
                    map.put("currentMonthTotal",currentMonthTotal);
                }
            }
            return map;
        }catch (Exception e){
            logger.info("获取现场取号和预约取号数失败"+e.getMessage());
            map.put("orderDayTotal",30);
            map.put("orderMonthTotal",200);
            map.put("currentDayTotal",50);
            map.put("currentMonthTotal",1000);
            return map;
        }
    }

    /**
     * 汇总近七天的预约数
     * @return
     */
    @Override
    public Map<String, Integer> totalLateOrder() {
        String pattern = "yyyy-MM-dd";
        Map<String,Integer> map = new LinkedHashMap<>();
        try {
            int notWorkDay = DateUtil.getNotWorkDay(7);
            for (int i = 7+notWorkDay; i>0; i--){
                Date start = DateUtil.getLateWorkDay(i,0,0,0);
                Date end = DateUtil.getLateWorkDay(i,23,59,59);
                if (start != null && end != null){
                    int num = takeNumberMapper.totalLateOrder(start,end);
                    map.put(new SimpleDateFormat(pattern).format(start),num);
                }
            }
            return map;
        }catch (Exception e) {
           logger.info("获取仅七天预约数据失败"+e.getMessage());
           return null;
        }
    }

    @Override
    public Map<String, Integer> totalLateCurrent() {
        String pattern = "yyyy-MM-dd";
        Map<String,Integer> map = new LinkedHashMap<>();
        try {
            int notWorkDay = DateUtil.getNotWorkDay(7);
            for (int i = 7+notWorkDay; i>0; i--){
                Date start = DateUtil.getLateWorkDay(i,0,0,0);
                Date end = DateUtil.getLateWorkDay(i,23,59,59);
                if (start != null && end != null) {
                    int num = takeNumberMapper.totalLateCurrent(start, end);
                    map.put(new SimpleDateFormat(pattern).format(start), num);
                }
            }
            return map;
        }catch (Exception e) {
            /**
             *  随机返回默认数据类型
             * 1.返回300以内的随机数据
             * 2.以起始数据开始每天增加一定数值
             */
            Random random = new Random();
            if (random.nextBoolean()) {
                for (int i = 7; i > 0; i--) {
                    map.put("" + i + "", random.nextInt(300));
                }
                return map;
            }

            int a =280;
            for (int i=7; i>0; i--){
                map.put(""+i+"",a+=15);
            }
            return map;
        }
    }

    @Override
    public Map<String, Integer> totalLateWeiXin() {
        String pattern = "yyyy-MM-dd";
        Map<String,Integer> map = new LinkedHashMap<>();
        try {
            int notWorkDay = DateUtil.getNotWorkDay(7);
            for (int i = 7+notWorkDay; i>0; i--){
                Date start = DateUtil.getLateWorkDay(i,0,0,0);
                Date end = DateUtil.getLateWorkDay(i,23,59,59);
                if (start != null && end != null) {
                    int num = takeNumberMapper.totalLateWeiXin(start, end);
                    map.put(new SimpleDateFormat(pattern).format(start), num);
                }
            }
            return map;
        }catch (Exception e) {
            /**
             *  随机返回默认数据类型
             * 1.返回300以内的随机数据
             * 2.以起始数据开始每天增加一定数值
             */
            Random random = new Random();
            if (random.nextBoolean()) {
                for (int i = 7; i > 0; i--) {
                    map.put("" + i + "", random.nextInt(300));
                }
                return map;
            }

            int a =280;
            for (int i=7; i>0; i--){
                map.put(""+i+"",a+=15);
            }
            return map;
        }
    }
}
